<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <!-- <img src="../assets/logo.png" alt="" /> -->
        <img src="../../public/images/logo.png" alt="" />
      </div>
      <!-- 登陆表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
        ref="loginForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-s-custom"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            maxlength="15"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import "@/assets/fonts/iconfont.css";
import { loginAPI } from '@/api'
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名~', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码~', trigger: 'blur' },
          {
            min: 5,
            max: 15,
            message: '长度在 5 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //   登陆方法
    submitLoginForm() {
      // a.再次校验表单元素
      this.$refs.loginForm.validate(async (valid) => {
        //   b.如果校验未通过,则停止执行
        if (!valid) return
        // c.如果校验通过,则获取表单数据,发送给登陆接口
        const loginFormData = { ...this.loginForm }
        // d.获取接口返回的响应报文体数据，并命名为res
        const { data: res } = await loginAPI(loginFormData)
        // e.先显示结果消息（成功/失败）
        this.$message.warning(res.meta.msg)
        // f.如果失败，则停止执行
        if (res.meta.status !== 200) return
        // this.$message.error(res.meta.msg);
        // g.如果成功，则保存token，并跳转到首页
        // this.$message.success("登陆成功！");
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    resetLoginForm() {
      this.$refs.loginForm.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  //   background: url("../../public/images/dbgc.jpg") no-repeat 100% center;
}

.login_box {
  width: 450px;
  height: 300px;
  //   background-color: #fff;
  background: url('../../public/images/loginbgc.png') no-repeat 100% center;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  border: 1px solid #eee;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

// 表单样式
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

// 按钮区
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>